// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   TargetZone.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartGraphics;

// Referenced classes of package com.java4less.rchart:
//			ChartLabel, ChartLoader, Axis, Scale, 
//			FillStyle, LineStyle, Chart

public class TargetZone
{

	public static int DISABLED = -1;
	public int unitStart;
	public int unitEnd;
	public double positionStart;
	public LineStyle style;
	public double positionEnd;
	public FillStyle fillStyle;
	public String label;
	public boolean vertical;
	public boolean background;
	protected Chart chart;
	protected int effect3D;

	public TargetZone(double start, double end, int uStart, int uEnd)
	{
		unitStart = 0;
		unitEnd = 0;
		positionStart = DISABLED;
		style = null;
		positionEnd = DISABLED;
		label = "";
		vertical = false;
		background = true;
		chart = null;
		effect3D = 0;
		unitStart = uStart;
		unitEnd = uEnd;
		positionStart = start;
		positionEnd = end;
	}

	public static TargetZone createFromString(String s)
	{
		if (s.length() == 0)
			return null;
		String items[] = ChartLoader.convertList(s, ";");
		double start = DISABLED;
		double end = DISABLED;
		int uStart = 0;
		int uEnd = 0;
		try
		{
			if (items.length > 0)
				if (items[0].endsWith("%"))
				{
					start = (new Double(items[0].substring(0, items[0].length() - 1))).doubleValue();
					uStart = 1;
				} else
				{
					try
					{
						start = (new Double(items[0])).doubleValue();
					}
					catch (Exception ignore) { }
				}
			if (items.length > 1)
				if (items[1].endsWith("%"))
				{
					end = (new Double(items[1].substring(0, items[1].length() - 1))).doubleValue();
					uEnd = 1;
				} else
				{
					try
					{
						end = (new Double(items[1])).doubleValue();
					}
					catch (Exception ignore) { }
				}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		TargetZone zone = new TargetZone(start, end, uStart, uEnd);
		if (items.length > 2)
			zone.style = ChartLoader.convertLineStyle(items[2]);
		if (items.length > 3)
			zone.fillStyle = ChartLoader.convertFillStyle(items[3]);
		if (items.length > 4)
			zone.label = items[4];
		if (items.length > 5)
			zone.vertical = ChartLoader.convertBooleanParam(items[5], false);
		if (items.length > 6)
			zone.background = ChartLoader.convertBooleanParam(items[6], false);
		return zone;
	}

	protected void paint(ChartGraphics g, Axis xaxis, Axis yaxis)
	{
		int pixelStart = 0;
		Axis axis = yaxis;
		if (vertical)
			axis = xaxis;
		if (positionStart != (double)DISABLED)
			if (unitStart == 1)
			{
				double tmp = ((axis.scale.max - axis.scale.min) * positionStart) / 100D;
				pixelStart = axis.scale.getScreenCoord(tmp);
			} else
			{
				if (positionStart < axis.scale.min)
					positionStart = axis.scale.min;
				if (positionStart > axis.scale.max)
					positionStart = axis.scale.max;
				pixelStart = axis.scale.getScreenCoord(positionStart);
			}
		int pixelEnd = 0;
		if (positionEnd != (double)DISABLED)
			if (unitEnd == 1)
			{
				double tmp = ((axis.scale.max - axis.scale.min) * positionEnd) / 100D;
				pixelEnd = axis.scale.getScreenCoord(tmp);
			} else
			{
				if (positionEnd < axis.scale.min)
					positionEnd = axis.scale.min;
				if (positionEnd > axis.scale.max)
					positionEnd = axis.scale.max;
				pixelEnd = axis.scale.getScreenCoord(positionEnd);
			}
		if (vertical)
		{
			int hStart = yaxis.y;
			int hEnd = yaxis.y + yaxis.height;
			if (fillStyle != null && pixelEnd >= 0)
			{
				int y1 = hStart;
				int x1 = pixelStart;
				int y2 = hEnd;
				int x2 = pixelEnd;
				if (y2 < y1)
				{
					y2 = hStart;
					y1 = hEnd;
				}
				if (x2 < x1)
				{
					x1 = pixelEnd;
					x2 = pixelStart;
				}
				fillStyle.draw(g, x1 + effect3D, y1 - effect3D, x2 + effect3D, y2 - effect3D);
			}
			if (style != null && pixelStart >= 0)
				style.draw(g, pixelStart + effect3D, hStart - effect3D, pixelStart + effect3D, hEnd - effect3D);
			if (style != null && pixelEnd >= 0)
				style.draw(g, pixelEnd + effect3D, hStart - effect3D, pixelEnd + effect3D, hEnd - effect3D);
			if (label.length() > 0)
			{
				ChartLabel clabel = new ChartLabel((new StringBuilder()).append(" ").append(label).toString(), "", false, false);
				clabel.initialize(g, chart);
				clabel.paint(g, pixelStart + effect3D, hEnd - effect3D, -1, -1);
			}
		} else
		{
			int wStart = xaxis.x;
			int wEnd = xaxis.x + xaxis.width;
			if (fillStyle != null && pixelEnd >= 0)
			{
				int x1 = wStart;
				int y1 = pixelStart;
				int x2 = wEnd;
				int y2 = pixelEnd;
				if (x2 < x1)
				{
					x2 = wStart;
					x1 = wEnd;
				}
				if (y2 < y1)
				{
					y1 = pixelEnd;
					y2 = pixelStart;
				}
				fillStyle.draw(g, x1 + effect3D, y1 - effect3D, x2 + effect3D, y2 - effect3D);
			}
			if (style != null && pixelStart >= 0)
				style.draw(g, wStart + effect3D, pixelStart - effect3D, wEnd + effect3D, pixelStart - effect3D);
			if (style != null && pixelEnd >= 0)
				style.draw(g, wStart + effect3D, pixelEnd - effect3D, wEnd + effect3D, pixelEnd - effect3D);
			if (label.length() > 0)
			{
				ChartLabel clabel = new ChartLabel((new StringBuilder()).append(" ").append(label).toString(), "", false, false);
				clabel.initialize(g, chart);
				clabel.paint(g, wEnd + effect3D, pixelStart - effect3D, -1, -1);
			}
		}
	}

}
